package code.ws_common_com.core
{
	import code.StarlingMain;
	
	import feathers.display.Scale9Image;
	import feathers.textures.Scale9Textures;
	
	import flash.geom.Rectangle;
	
	import starling.display.Sprite;

	/**
	 * 进度条 
	 * @author qiangchen
	 * 
	 */	
	public class Progress extends Sprite
	{
		/** 进度条皮肤 */
		private var backgroundSkin:Scale9Image = null;
		/** 进度条填充皮肤 */
		private var fillSkin:Scale9Image = null;
		
		
		/**
		 * constructor 
		 * @param bgSkin
		 * @param fSkin
		 * @param rectBg
		 * @param rectFill
		 * 
		 */		
		public function Progress( bgSkin:String, fSkin:String, rectBg:Rectangle, rectFill:Rectangle )
		{
			var bgTexture:Scale9Textures = new Scale9Textures( StarlingMain.instance.assets.getTextureAtlas("ui").getTexture(bgSkin), rectBg );
			backgroundSkin = new Scale9Image( bgTexture );
			addChild( backgroundSkin );
			
			var fillTexture:Scale9Textures = new Scale9Textures( StarlingMain.instance.assets.getTextureAtlas("ui").getTexture(fSkin), rectFill );
			fillSkin = new Scale9Image( fillTexture );
			addChild( fillSkin );
			
			resetPostion();
		}
		
		
		private function resetPostion():void{
			fillSkin.x = backgroundSkin.x + 2 + backgroundSkin.width - fillSkin.width >> 1;
			fillSkin.y = backgroundSkin.y + 2 + backgroundSkin.height - fillSkin.height >> 1;
		}
		
		/**
		 * set background & fill skin's size
		 * @param bgW
		 * @param fillW
		 * 
		 */		
		public function setSize( bgW:int, fillW:int ):void
		{
			bgW = (bgW < backgroundSkin.width) ? backgroundSkin.width : bgW;
			fillW = (fillW < fillSkin.width) ? fillSkin.width : fillW;
			
			backgroundSkin.width = bgW;
			fillSkin.width = fillW;
			
			resetPostion();
		}
		
		
		/**
		 * set value to show 
		 * @param value ( range: 0.0 - 1.0 );
		 * 
		 */		
		public function setValue( value:Number ):void
		{
			if( value > 1 ) 
			{
				throw new Error("Error---> Progress set value is error! value > 1");
				return;
			}
			
			fillSkin.scaleX = value;
			value = NaN;
		}
	}
}